<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cherry Editor - Markdown Editor</title>
    <link rel="stylesheet" href="cherry-markdown/cherry-markdown.min.css">
</head>

<body>
<div id="markdown-editor"></div>
</body>
<script src="cherry-markdown/cherry-markdown.js"></script>
<script>
    window.onload = function(){
        init();
    }

    let lastModifyTime = new Date().getTime();

    function init(){
        //初始化代码，固定格式
        var config = {
            id: 'markdown-editor',
            value: '',
            callback: {
                afterChange: function (md, html) {
                    lastModifyTime = new Date().getTime();
                    debounce(() => save(md), 2000)
                }
            },
        };
        const md = new Cherry(config);

        window.setMarkdown = function(content) {
            md.setMarkdown(content);
        }

        function save(md) {
            console.log('java' + window.java)
            window.java.setContent(md);
        }

        // 防抖函数：延迟执行，直到用户停止操作一段时间
        function debounce(func, delay) {
            // 每次触发时清除前一个定时器
            if (window.timer) {
                clearTimeout(window.timer);
            }
            // 设置新的定时器，只有在延迟时间内没有再次触发时才执行
            window.timer = setTimeout(() => {
                func();
            }, delay);
        }
    }

    window.intervalId = setInterval(function(){
        let now = new Date().getTime();
        let distance = now - lastModifyTime;
        if((distance / 1000) > 60){
            // 超过了60秒
            clearInterval(window.intervalId);
            java.doHide();
        }
    }, 2000)



</script>

</html>